package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.buildtable.pojo.Columns;
import com.example.buildtable.pojo.Newsreport;
import com.example.common.util.R;
import com.example.buildtable.service.NewsreportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@Tag(name = "新闻资讯")
@RequestMapping(value = "/newsreport/")
public class NewsreportController {
    @Autowired
    private NewsreportService newsreportService;

    @Operation(summary = "查询所有新闻资讯数据")
    @GetMapping(value = "/getAllNewsreport")
    public R getAllNewsreport() {
        LambdaJoinQueryWrapper<Newsreport> wrapper = new LambdaJoinQueryWrapper<>(Newsreport.class);
        wrapper.innerJoin(Columns.class, Columns::getColumnId, Newsreport::getNewsColumnId);
        List<Newsreport> newsreportList = newsreportService.list(wrapper);
        return R.ok("查询所有新闻资讯数据成功").data("newsreportList", newsreportList);
    }

    @Operation(summary = "查询所有新闻资讯模糊查询带分页")
    @PostMapping(value = "/getNewsreportPage/{page}/{limit}")
    public R getNewsreportPage(@PathVariable("page") Long page, @PathVariable("limit") Long limit,  @RequestParam(name = "newsTitle", required = false) String newsTitle) {
        LambdaJoinQueryWrapper<Newsreport> wrapper = new LambdaJoinQueryWrapper<>(Newsreport.class);
        wrapper.innerJoin(Columns.class, Columns::getColumnId, Newsreport::getNewsColumnId);
        wrapper.like(newsTitle != null && !newsTitle.equals(""), Newsreport::getNewsTitle, newsTitle);
        Page<Newsreport> newsreportPage = new Page<>(page, limit);
        newsreportService.page(newsreportPage, wrapper);
        //每页显示的记录
        List<Newsreport> newsreportList = newsreportPage.getRecords();
        //共多少条记录
        Long total = newsreportPage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", newsreportList);
        return R.ok("查询所有新闻资讯模糊查询带分页成功").data("map", map);
    }

    @Operation(summary = "增加新闻资讯")
    @PostMapping(value = "/addNewsreport")
    public R addNewsreport(Newsreport newsreport) {
        boolean flag = newsreportService.save(newsreport);
        if (flag) {
            return R.ok("新闻资讯增加成功");
        } else {
            return R.ok("新闻资讯增加失败");
        }
    }

    @Operation(summary = "删除新闻资讯")
    @GetMapping(value = "/deleteNewsreport/{newsId}")
    public R deleteNewsreport(@PathVariable("newsId") Integer newsId) {
        boolean flag = newsreportService.removeById(newsId);
        if (flag) {
            return R.ok("新闻资讯删除成功");
        } else {
            return R.ok("新闻资讯删除失败");
        }
    }

    @Operation(summary = "按照新闻资讯查看")
    @GetMapping(value = "/getNewsreportById/{newsId}")
    public R getNewsreportById(@PathVariable("newsId") Integer newsId) {
        LambdaJoinQueryWrapper<Newsreport> wrapper = new LambdaJoinQueryWrapper<>(Newsreport.class);
        wrapper.innerJoin(Columns.class, Columns::getColumnId, Newsreport::getNewsColumnId);
        wrapper.eq(Newsreport::getNewsId, newsId);
        Newsreport newsreport = newsreportService.getOne(wrapper);
        if (newsreport != null) {
            return R.ok("按照新闻资讯查看成功").data("newsreport", newsreport);
        } else {
            return R.error("按照新闻资讯查看失败");
        }
    }

    @Operation(summary = "修改新闻资讯")
    @PostMapping(value = "/updateNewsreport")
    public R updateNewsreport(Newsreport newsreport) {
        boolean flag = newsreportService.updateById(newsreport);
        if (flag) {
            return R.ok("新闻资讯修改成功");
        } else {
            return R.ok("新闻资讯修改失败");
        }
    }
}